﻿
Imports System.Data
Imports VSModel
Imports Cls_Common
Partial Class FrontEnd_Control_Menu_DPMenu
    Inherits System.Web.UI.UserControl
    Public curStoreURL As String = ""

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If Not Page.RouteData.Values("CateId") Is Nothing Then
                hidRootCat.Value = Page.RouteData.Values("CateId")
   


            End If

            LoadMenu()
        End If
    End Sub
    Protected Sub LoadMenu()
        Using data As New VSEntities
            Dim k = (From q In data.Categories Where q.ParentId = 0 Order By q.Sort Select q).ToList
            If hidRootCat.Value <> "" Then
                hidRootCat.Value = getAllMenuIdInCate(hidRootCat.Value)
            ElseIf Not Page.RouteData.Values("ProductId") Is Nothing AndAlso Page.RouteData.Values("ProductId") <> "" Then
                'check xem phai la Product
                Dim ProId As Integer = Page.RouteData.Values("ProductId")
                Dim _Cate As Integer = (From pro In data.Products Where pro.ProductId = ProId Select pro.CateID).FirstOrDefault
                hidRootCat.Value = getAllMenuIdInCate(_Cate)
            End If

            With rptMenu
                .DataSource = k
                .DataBind()
            End With


        End Using
    End Sub

    Protected Sub rptMenu_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptMenu.ItemDataBound
        Dim row As VSModel.Category = CType(e.Item.DataItem, VSModel.Category)

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.NavigateUrl = MainDomain & "san-pham/" & row.QueryString & "-" & row.CatId.ToString & ".html"
        hplTitle.Text = row.Title.ToString

        'bind child menu
        Dim item As RepeaterItem = e.Item
        If item.ItemType = ListItemType.Item Or item.ItemType = ListItemType.AlternatingItem Then
            Dim rptChild = CType(item.FindControl("rptMenuChild"), Repeater)
            Using data As New VSEntities
                Dim fn = (From q In data.Categories Where q.ParentId = row.CatId).ToList
                With rptChild
                    .DataSource = fn
                    .DataBind()
                End With
            End Using
        End If
        If hidRootCat.Value.ToString.Contains(row.CatId) Then
            hplTitle.CssClass = "active_parent"
        End If

        'end bind menu

    End Sub
    Protected Sub rptMenuChild_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)

        Dim row As VSModel.Category = CType(e.Item.DataItem, VSModel.Category)
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.NavigateUrl = MainDomain & "san-pham/" & row.QueryString & "-" & row.CatId.ToString & ".html"
        hplTitle.Text = row.Title.ToString
        If hidRootCat.Value.ToString.Contains(row.CatId) Then
            hplTitle.CssClass = "active_sub"
        End If

    End Sub

    Function getAllMenuIdInCate(ByVal _CateId As Integer) As String
        Using data As New VSEntities
            Dim k = (From q In data.Categories Where q.CatId = _CateId Select q).FirstOrDefault
            If Not k Is Nothing AndAlso k.ParentId <> 0 Then
                Return k.CatId.ToString & ";" & getAllMenuIdInCate(k.ParentId)
            Else
                Return k.CatId
            End If

        End Using


    End Function

End Class
